﻿#include "plotchartutils.h"


void PlotChartUtils::writeConfData(QString path, QList<ConfData> listConf)
{

    QSettings settings(path, QSettings::IniFormat);
    for(int lIndex = 0; lIndex < listConf.length(); lIndex++)
    {
        ConfData confData = listConf.at(lIndex);
//        qDebug() << "[" << __FUNCTION__ <<__LINE__ << "] :" <<  confData.point;
        settings.beginGroup(confData.name);

        settings.setValue(INI_KEY_INDEX, confData.index);
        settings.setValue(INI_KEY_NAME, confData.name);
        settings.setValue(INI_KEY_DATA, listPointToStr(confData.point));

        settings.endGroup();
    }
}

QList<ConfData> PlotChartUtils::getConfData()
{
    QList<ConfData> listConf;
    QFile file(INI_PATH);

    if(file.exists())
    {
        QSettings settings(INI_PATH, QSettings::IniFormat);
        QStringList groups = settings.childGroups();

        for(int gIndex = 0; gIndex < groups.length(); gIndex++)
        {
            settings.beginGroup(groups.at(gIndex));

            ConfData confData;
            confData.index = qAbs(settings.value(INI_KEY_INDEX).toInt());
            confData.name = settings.value(INI_KEY_NAME).toString();
            QString strPoint = settings.value(INI_KEY_DATA).toString();
            confData.point = strToListPoint(strPoint);
            listConf.append(confData);

            settings.endGroup();

        }

    }

    qSort(listConf.begin(), listConf.end(), [=](const ConfData &confData1,
          const ConfData &confData2){
        return confData1.index < confData2.index;
    });


    return listConf;

}

QList<ConfData> PlotChartUtils::getConfData(QString path)
{
    QList<ConfData> listConf;
    QFile file(path);

    if(file.exists())
    {
        QSettings settings(path, QSettings::IniFormat);
        QStringList groups = settings.childGroups();

        for(int gIndex = 0; gIndex < groups.length(); gIndex++)
        {
            settings.beginGroup(groups.at(gIndex));

            ConfData confData;
            confData.index = qAbs(settings.value(INI_KEY_INDEX).toInt());
            confData.name = settings.value(INI_KEY_NAME).toString();
            QString strPoint = settings.value(INI_KEY_DATA).toString();
            confData.point = strToListPoint(strPoint);
            listConf.append(confData);

            settings.endGroup();
        }
    }

    return listConf;
}

void PlotChartUtils::removeConfDataItem(QString path, QList<ConfData> listConf, int itemIndex)
{
    QSettings settings(path, QSettings::IniFormat);
    QStringList groups = settings.childGroups();

    for(int gIndex = 0; gIndex < groups.length(); gIndex++)
    {
        if(gIndex == itemIndex)
        {
            settings.beginGroup(groups.at(gIndex));
            QString name = settings.value(INI_KEY_NAME).toString();
            settings.endGroup();
            settings.remove(name);
        }
    }

    for(int lIndex = 0; lIndex < listConf.length(); lIndex++)
    {
        ConfData confData = listConf.at(lIndex);
        settings.beginGroup(confData.name);

        settings.setValue(INI_KEY_INDEX, confData.index);
        settings.setValue(INI_KEY_NAME, confData.name);
        settings.setValue(INI_KEY_DATA, listPointToStr(confData.point));

        settings.endGroup();
    }
}

QList<QString> PlotChartUtils::getLocalPathConfFileName(QString path)
{
    QDir dir(path);
    QStringList fileNames;
    fileNames.clear();

    if(dir.exists())
    {
        QStringList nameFilters;
        nameFilters << "*.ini";

        fileNames = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
        fileNames = fileNames.replaceInStrings(".ini", "");

    }
    return fileNames;
}



QList<QPointF> PlotChartUtils::strToListPoint(QString strPoint)
{
    QList<QPointF> listPoint;
    QStringList pointList = strPoint.split("-");

    if(!strPoint.isEmpty())
    {
        for(int i = 0; i < pointList.length(); i++)
        {
            QString pointItem = pointList.at(i);
            QString delStrBrackets = pointItem.mid(1, pointItem.length() - 2);
            QStringList strPointList = delStrBrackets.split(",");

            qreal pointX = QString(strPointList.at(0)).trimmed().toDouble();
            qreal pointY = QString(strPointList.at(1)).trimmed().toDouble();

            listPoint.append(QPointF(qAbs(pointX), qAbs(pointY)));

        }
    }
    return listPoint;
}

QString PlotChartUtils::listPointToStr(QList<QPointF> listPoint)
{
    QString strPoint;
    QStringList strPointList;

    for(int i = 0; i < listPoint.length(); i++)
    {
        QPointF pointF = listPoint.at(i);
        QString strPoint = QString("(%1, %2)").arg(pointF.x()).arg(pointF.y());
        strPointList.append(strPoint);
    }
    strPoint = strPointList.join("-");

    return strPoint;
}
